package juc.executors;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class ScheduledExecutorServiceExample2 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //testScheduleWithFixedDelay();
        //test1();
        test2();
    }

    private static void testScheduleWithFixedDelay() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);

        /**
         * 每隔2秒执行一次，但每个线程需要执行5秒，则会每隔7秒执行一次
         */
        /*ScheduledFuture<?> future = executor.scheduleWithFixedDelay(() -> {
            System.out.println("i am running");
            timeSleep(5);
        }, 1, 2, TimeUnit.SECONDS);*/

        final AtomicLong interval = new AtomicLong(0L);
        ScheduledFuture<?> future = executor.scheduleWithFixedDelay(() -> {
            long currentTimeMillis = System.currentTimeMillis();
            if (interval.get() == 0) {
                System.out.printf("the first time trigger task at %d\n", currentTimeMillis);
            } else {
                System.out.printf("the actually spend %d\n", currentTimeMillis - interval.get());
            }
            interval.set(currentTimeMillis);
            timeSleep(5);
        }, 1, 2, TimeUnit.SECONDS);
    }

    private static void test1() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);
        //默认false
        System.out.println(executor.getContinueExistingPeriodicTasksAfterShutdownPolicy());
        //true当线程池shutdown后schedule继续执行
        executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
        final AtomicLong interval = new AtomicLong(0L);
        ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> {
            long currentTimeMillis = System.currentTimeMillis();
            if (interval.get() == 0) {
                System.out.printf("the first time trigger task at %d\n", currentTimeMillis);
            } else {
                System.out.printf("the actually spend %d\n", currentTimeMillis - interval.get());
            }
            interval.set(currentTimeMillis);
            timeSleep(3);
        }, 1, 2, TimeUnit.SECONDS);
        timeSleep(1);
        executor.shutdown();
    }

    private static void test2() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);
        //默认true
        System.out.println(executor.getExecuteExistingDelayedTasksAfterShutdownPolicy());
        //true或false 当线程池shutdown后schedule不继续执行
        executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
        final AtomicLong interval = new AtomicLong(0L);
        ScheduledFuture<?> future = executor.scheduleWithFixedDelay(() -> {
            long currentTimeMillis = System.currentTimeMillis();
            if (interval.get() == 0) {
                System.out.printf("the first time trigger task at %d\n", currentTimeMillis);
            } else {
                System.out.printf("the actually spend %d\n", currentTimeMillis - interval.get());
            }
            interval.set(currentTimeMillis);
            timeSleep(3);
        }, 1, 2, TimeUnit.SECONDS);
        timeSleep(1);
        executor.shutdown();

    }

    private static void timeSleep(long time) {
        try {
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
